import nodeResolve from "@rollup/plugin-node-resolve"
import commonjs from "@rollup/plugin-commonjs"
import babel from "@rollup/plugin-babel"
import json from "@rollup/plugin-json"
import { terser } from "rollup-plugin-terser"
import serve from "rollup-plugin-serve"
import livereload from "rollup-plugin-livereload"
import nodePolyfills from "rollup-plugin-node-polyfills"

import minimist from "minimist"

const config = {
  input: ["./src/index.js"],
  output: [
    {
      file: "./dist/ScrewScanQrcode.js",
      format: "umd",
      name: "ScrewScanQrcode",
      sourcemap: true,
    },
  ],
  plugins: [commonjs(), babel(), nodePolyfills(), nodeResolve({ browser: true }), json()],
}

const argv = minimist(process.argv.slice(2))
const developFlag = argv.develop
const buildFlag = argv.build

//开发模式
if (developFlag === true) {
  config.plugins = config.plugins.concat([
    serve({
      contentBase: "", //服务器启动的文件夹，默认是项目根目录，需要在该文件下创建index.html
      port: 8020, //端口号，默认10001
    }),
    livereload("dist"),
  ])
}

//发布模式
if (buildFlag === true) {
  config.plugins = config.plugins.concat([terser()])
}

export default config
